﻿using Sl.Upload.Models;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Sl.Upload.Help
{
    /// <summary>
    /// 上传图片处理
    /// </summary>
    public class ImageHelp
    {
        #region 截剪缩略图
        /// <summary>
        /// 截剪图片
        /// </summary>
        /// <param name="fileStream">文件流</param>
        /// <param name="ci">文件生成信息</param>
        /// <param name="wat">水印</param>
        public void ToThumImages(Stream fileStream, CutInfo ci)
        {
            if (fileStream == null) throw new NullReferenceException("图片源文件参数为空！");
            Image image = Image.FromStream(fileStream, true);
            int thumWidth = 0, thumHeight = 0;
            int zoom = (ci.Width > ci.Height) ? ci.Height : ci.Width;
            int oriWidth = ci.Mode ? image.Width : (ci.RatioWidth == 0 ? image.Width : ci.RatioWidth);
            int oriHeight = ci.Mode ? image.Height : (ci.RatioHeight == 0 ? image.Height : ci.RatioHeight);
            #region 缩放宽高
            if (ci.Mode)
            {
                if (ci.Width == 0 && ci.Height == 0)
                {
                    thumWidth = image.Width;
                    thumHeight = image.Height;
                }
                else
                {
                    if (((float)oriWidth / (float)oriHeight) > ((float)ci.Width / (float)ci.Height))
                    {
                        thumWidth = ci.Width;
                        thumHeight = (int)(oriHeight * ((float)ci.Width / (float)oriWidth));
                    }
                    else
                    {
                        thumHeight = ci.Height;
                        thumWidth = (int)(oriWidth * ((float)(ci.Height) / (float)(oriHeight)));
                    }
                }
            }
            else
            {
                if (oriWidth > ci.Width && oriHeight > ci.Height)
                {
                    thumWidth = ci.Width;
                    thumHeight = ci.Height;
                }
                else if (oriWidth < ci.Width && oriHeight < ci.Height)
                {
                    thumWidth = oriWidth;
                    thumHeight = oriHeight;
                }
                else if (oriWidth > ci.Width && oriHeight < ci.Height)
                {
                    thumWidth = ci.Width;
                    thumHeight = oriHeight;
                }
                else if (oriWidth < ci.Width && oriHeight > ci.Height)
                {
                    thumWidth = oriWidth;
                    thumHeight = ci.Height;
                }
                else
                {
                    thumWidth = ci.Width;
                    thumHeight = ci.Height;
                }
            }
            #endregion
            Bitmap bitmap = new Bitmap(thumWidth, thumHeight, PixelFormat.Format32bppArgb);
            //从指定的 Image 对象创建新 Graphics 对象
            Graphics graphics = Graphics.FromImage(bitmap);
            //设置高质量插值法
            graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            //设置高质量,低速度呈现平滑程度
            graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            graphics.CompositingMode = System.Drawing.Drawing2D.CompositingMode.SourceCopy;
            graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
            //清除整个绘图面并以透明背景色填充
            if (image.RawFormat.Guid == ImageFormat.Jpeg.Guid)
                graphics.Clear(Color.White);
            else
                graphics.Clear(Color.Transparent);
            if (ci.Mode)
                graphics.DrawImage(image, new Rectangle(0, 0, thumWidth, thumHeight));
            else
            {
                Bitmap bit = ZoomImage(image, oriWidth, oriHeight);
                //graphics.DrawImage(bit, new Rectangle(0, 0, thumWidth, thumHeight), new Rectangle(ci.X, ci.Y, thumWidth, thumHeight), GraphicsUnit.Pixel);
                int w = oriWidth > ci.X ? oriWidth - ci.X : 0;
                int h = oriHeight > ci.Y ? oriHeight - ci.Y : 0;
                graphics.DrawImage(bit, new Rectangle(0, 0, w, h), new Rectangle(ci.X, ci.Y, w, h), GraphicsUnit.Pixel);
            }
            try
            {
                ImageCodecInfo ici = GetCodecInfo(image.RawFormat.Guid);
                EncoderParameters ep = new EncoderParameters(1);
                ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)80);
                //将此 原图片 以指定格式并用指定的编解码参数保存到指定文件
                if (ici != null)
                    bitmap.Save(ci.GeneratePath, ici, ep);
                else
                    bitmap.Save(ci.GeneratePath, image.RawFormat);
            }
            catch (System.Exception e)
            {
                throw e;
            }
            finally
            {
                image.Dispose();
                bitmap.Dispose();
                graphics.Dispose();
            }
        }

        public Bitmap ZoomImage(Image image, int thumWidth, int thumHeight)
        {
            Bitmap bitmap = new Bitmap(thumWidth, thumHeight, PixelFormat.Format32bppArgb);
            //从指定的 Image 对象创建新 Graphics 对象
            Graphics graphics = Graphics.FromImage(bitmap);
            //设置高质量插值法
            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            //设置高质量,低速度呈现平滑程度
            graphics.SmoothingMode = SmoothingMode.HighQuality;
            graphics.CompositingMode = CompositingMode.SourceCopy;
            graphics.CompositingQuality = CompositingQuality.HighQuality;
            graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
            //清除整个绘图面并以透明背景色填充
            if (image.RawFormat.Guid == ImageFormat.Jpeg.Guid)
                graphics.Clear(Color.White);
            else
                graphics.Clear(Color.Transparent);
            graphics.DrawImage(image, new Rectangle(0, 0, thumWidth, thumHeight));
            return bitmap;
        }
        #endregion

        #region 获取图像编码解码器的所有相关信息
        /// <summary>
        /// 取得编码
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        protected ImageCodecInfo GetCodecInfo(Guid guid)
        {
            ImageCodecInfo ici = null;
            ImageCodecInfo[] icis = ImageCodecInfo.GetImageEncoders();
            if (guid == ImageFormat.Bmp.Guid)
                ici = icis[0];
            else if (guid == ImageFormat.Jpeg.Guid)
                ici = icis[1];
            else if (guid == ImageFormat.Gif.Guid)
                ici = icis[2];
            else if (guid == ImageFormat.Tiff.Guid)
                ici = icis[3];
            else if (guid == ImageFormat.Png.Guid)
                ici = icis[4];
            return ici;
        }
        #endregion

        #region 保存图片
        /// <summary>
        /// 保存图片
        /// </summary>
        /// <param name="fileStream">保存图片流</param>
        /// <param name="savePath">图片路径</param>
        /// <param name="wat">是否添加水印</param>
        /// <returns></returns>
        public bool SaveImage(Stream fileStream, string savePath)
        {
            bool rel = true;
            Image image = Image.FromStream(fileStream, true);
            ImageCodecInfo ici = GetCodecInfo(image.RawFormat.Guid);
            try
            {
                //检查目录是否存在,不存在并创建目录
                if (image.RawFormat.Equals(ImageFormat.Gif))
                {
                    image.Save(savePath, ImageFormat.Gif);
                }
                else
                {
                    image.Save(savePath, image.RawFormat);
                }
            }
            catch (Exception ex)
            {
                rel = false;
                throw new Exception(ex.Message);
            }
            return rel;
        }
        #endregion
    }
}
